package com.xiaomaoguai.webkit.web.listener;

import java.util.Enumeration;
import java.util.Properties;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils;

import com.xiaomaoguai.webkit.core.utils.XmgStringUtils;
import com.xiaomaoguai.webkit.io.ResourceUtils;
import com.xiaomaoguai.webkit.web.WebConstants;
import com.xiaomaoguai.webkit.web.XmgWebUtils;

/**
 * @Description :
 * @FileName: ApplicationEnvironmentContextListener.java
 * @Author :WeiHui.Zhang
 * @Data : 2015年5月8日 下午3:08:55
 * @Version:V1.00
 */
public class ApplicationEnvironmentContextListener implements ServletContextListener{

	@Override
	public void contextInitialized(ServletContextEvent contextEvent) {
        ServletContext context = contextEvent.getServletContext();
		context.log("第4加载的Listener");
        String environmentConfigLocation = XmgWebUtils.getInitParameter(context, "environmentConfigLocation");
        if (StringUtils.isEmpty(environmentConfigLocation)) {
            context.log("No environment config location is found");
            return;
        }
        try {
            Properties properties = new Properties();
            for (String location : XmgStringUtils.tokenizeToStringArray(environmentConfigLocation, WebConstants.DELIMITERS)) {
            	Resource[] resources = ResourceUtils.getResources(location);
                if (ArrayUtils.isEmpty(resources)) {
                    context.log("no resources is found with environment location[" + location + "].");
                    continue;
                }
                for (Resource resource : resources) {
                    try {
                    	System.out.println("resource=="  + resource);
                        PropertiesLoaderUtils.fillProperties(properties, resource);
                    } catch (Exception e) {
                        context.log("load environment properties failure with resource[" + resource + "].", e);
                    }
                }
            }
            @SuppressWarnings("unchecked")
            Enumeration<String> names = (Enumeration<String>)properties.propertyNames();
            while(names.hasMoreElements()) {
                String name = names.nextElement();
                String value = properties.getProperty(name);
                if(System.getProperties().contains(name)) {
                    String cached = System.getProperty(name);
                    context.log("replace exit system property[" + name + "] " + cached + " with new value[" + value +"]");
                } else {
                    context.log("set system property[" + name + "] with value[" + value +"]");
                }
                System.setProperty(name, value);
            }
        } catch (Exception e) {
            context.log("load environment failure.", e);
        }
	}

	@Override
	public void contextDestroyed(ServletContextEvent sce) {
	}

}